<?php
defined('BASEPATH') OR exit('No direct script access allowed');
// +----------------------------------------------------------------------
// | 用户登录注册逻辑层
// +----------------------------------------------------------------------
// | Copyright (c) 2015
// +----------------------------------------------------------------------
// | Author: baiping 125618036@qq.com http://www.webipcode.com
// +----------------------------------------------------------------------
// | 2015-7-29下午3:34:15
// +----------------------------------------------------------------------

class Authentication extends CI_Controller{
	
	public function __construct(){
		parent::__construct();
		$this->load->helper('common');
	}
	
	/**
	 * 用户登录
	 * @param string $username 用户名
	 * @param string $password 密码
	 * @param string $verify 验证码
	 */
	public function login(){
		if ( IS_POST ){
			$this->load->library('form_validation');
			//验证表单
			if( $this->form_validation->run('login')){
				//post接收参数
				$username =  $this->input->post('username');
				$password =  $this->input->post('password');
				$verify =  $this->input->post('verify');
				if ( false == check_verify( $verify ) ){
					show_message('error', '验证码错误', site_url());
					return false;
				}
				$this->load->model('Member_model', 'member');//载入member类
				$status = $this->member->login($username, $password);
				if ( 0 > $status ){
					show_message('error', $this->showMemberErr($status), site_url());
				}else{
					header("location:".site_url('main/index'));
				}
				
			}else{
				$this->load->vars('meta_title', '管理员登录');
				$this->load->view('index/index');
			}
		}else{
			$this->show_message('error', '非法请求', site_url());
		}
	}
	
	/**
	 * 退出
	 */
	public function logout(){
		if(is_login()){
			$this->load->model('Member_model', 'member');
			$this->member->logout();
			show_message('success', '退出成功', base_url());
		}else{
			redirect(site_url());
		}
	}
	/**
	 * 获取用户的错误信息
	 * @param unknown_type $code
	 */
	private function showMemberErr($code=0){
		switch ($code) {
			case -2:  $error = '用户已经锁定'; break;
			case -3:  $error = '用户名被删除'; break;
			case -5:  $error = '没有这个帐号'; break;
			case -10:  $error = '密码错误'; break;
			default:  $error = '未知错误';
		}
		return $error;
	}
}